define(['jquery', 'bootstrap', 'agent', 'table', 'form'], function ($, undefined, Agent, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order_aftersales/index',
                    multi_url: 'order/multi',
                },
                table: 'order_aftersales',
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: false,
                columns: [
                    [
                        {field: 'id', title: 'ID',operate: false},
                        {field: 'member.member_name',title:'客户名称',operate: 'LIKE'},
                        {field: 'shop.shop_name',title:'商家名称', operate: 'LIKE'},
                        {field: 'order.order_sn', title: '订单编号',operate: 'LIKE'},
                        {field: 'order.order_amount',title:'订单总额',operate: false,sortable: true},
                        {field: 'order.coupon_money', title: '优惠券金额',operate: false,sortable: true},
                        {field: 'refund',title:'退款金额',operate: false,sortable: true},
                        {field: 'reason',title:'退款原因',operate: false},
                        {field: 'status',title:'状态',formatter: Table.api.formatter.status, custom:{0: '待审核', 1: '审核失败',2:'审核通过',3:'退款成功',4:'退款失败'},searchList: {0: '待审核', 1: '审核失败',2:'审核通过',3:'退款成功',4:'退款失败'}},
                        {field: 'createtime', title: '申请时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            clickToSelect: false,
                            buttons: [{
                                name: '流程记录',
                                title: __('流程记录'),
                                classname: 'btn btn-xs btn-primary btn-dialog btn-detail',
                                text: '流程记录',
                                icon: 'fa fa-sticky-note-o',
                                extend: 'data-area=\'["40%","80%"]\'',
                                url: 'order_aftersales/record'
                            }]
                        }
                    ]
                ]
            });

            var options = table.bootstrapTable('getOptions');
            var queryParams = options.queryParams;
            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    if (value) {
                        params.filter = JSON.stringify(value);
                        let op = {}
                        for (let i in value) {
                            op[i] = '=';
                        }
                        params.op = JSON.stringify(op);
                    }
                    params = queryParams.call(this, params);
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});